<?php

/**
 * Gestisce tutte le richieste di ricerca - LaFabbrica.org
 *
 * @created 11-giu-2012
 * @author     luigi.pastore (gigi@la-fabbrica.org)
 * @package    LaFabbrica
 * @copyright  Copyright (c) 2011 Luigi Pastore
 */
class SearchController extends Classes_Controller
{

    public function init()
    {
	$this->layout->disableLayout( true );
        
        $this->db = $this->application->db;

	// Parametri della richiesta
	$this->params = $this->getParams();
        
        $this->results = array();
    }

    /**
    * Search.
    * Sceglie la sezione in base ai parametri 'section' e 'searchString' passato da url
    * Eg: /search/section/searchString/param1/value1/param2/value2 ...
    */   
    public function searchAction()
    {
	if( empty( $this->params['section'] ) )
	    throw new Classes_Exceptions( 'Ajax->search: URL malformato' );

        $this->queryString = urldecode( $this->params['searchString'] );
        
	switch( $this->params['section'] )
	{
	    case 'artists':
		return $this->searchArtists();
		break;

            case 'artistsmini':
		return $this->searchArtistsMini();
		break;

            case 'posts':
		return $this->searchPosts();
		break;

            case 'contacts':
		return $this->searchContacts();
		break;

            case 'partners':
		return $this->searchPartners();
		break;

            case 'locations':
		return $this->searchLocations();
		break;

            case 'locationsmini':
		return $this->searchLocationsMini();
		break;

            case 'events':
		return $this->searchEvents();
		break;
            
	    default:
		break;
	}
	//sleep(2);	 
    }
    
    private function searchArtistsMini()
    {
        $query = $this->db->query( "SELECT artist_id FROM fab_artists WHERE artist_name LIKE '%". $this->queryString ."%'" );

        while( $row = $this->db->fetch( $query ) )
        {
            $this->results[] = $row['artist_id'];
        }
        
        $this->model_artist = new Classes_Artist;
        
        if( count( $this->results ) )
            $this->layout->artists = $this->model_artist->fetchById( $this->results );
        
        $this->layout->content = array( 'script' => 'search/artistsmini' );
    }
    
    private function searchPosts()
    {
        $query = $this->db->query( "SELECT post_id FROM fab_posts WHERE post_title LIKE '%". $this->queryString ."%'" );

        while( $row = $this->db->fetch( $query ) )
        {
            $this->results[] = $row['post_id'];
        }
        
        $this->model_post = new Classes_Post;
        
        if( count( $this->results ) )
            $this->layout->posts = $this->model_post->fetchById( $this->results );
        
        $this->layout->content = array( 'script' => 'search/posts' );
    }
    
    private function searchContacts()
    {
        $query = $this->db->query( "SELECT contact_id FROM fab_contacts WHERE contact_firstname LIKE '%". $this->queryString ."%' OR contact_lastname LIKE '%". $this->queryString ."%'" );

        while( $row = $this->db->fetch( $query ) )
        {
            $this->results[] = $row['contact_id'];
        }
        
        $this->model_contact = new Classes_Contact;
        
        if( count( $this->results ) )
            $this->layout->contacts = $this->model_contact->fetchById( $this->results );
        
        $this->layout->content = array( 'script' => 'search/contacts' );
    }
    
    private function searchPartners()
    {
        $query = $this->db->query( "SELECT partner_id FROM fab_partners WHERE partner_name LIKE '%". $this->queryString ."%'" );

        while( $row = $this->db->fetch( $query ) )
        {
            $this->results[] = $row['partner_id'];
        }
        
        $this->model_partner = new Classes_Partner;
        
        $this->layout->partner_types = $this->application->getResource( 'PartnerTypes' );
        
        if( count( $this->results ) )
            $this->layout->partners = $this->model_partner->fetchById( $this->results );
        
        $this->layout->content = array( 'script' => 'search/partners' );
    }
    
    private function searchLocations()
    {
        $query = $this->db->query( "SELECT location_id FROM fab_locations WHERE location_name LIKE '%". $this->queryString ."%'" );

        while( $row = $this->db->fetch( $query ) )
        {
            $this->results[] = $row['location_id'];
        }
        
        $this->model_location = new Classes_Location;
        
        if( count( $this->results ) )
            $this->layout->locations = $this->model_location->fetchById( $this->results );
        
        $this->layout->content = array( 'script' => 'search/locations' );
    }
    
    private function searchLocationsMini()
    {
        $query = $this->db->query( "SELECT location_id FROM fab_locations WHERE location_name LIKE '%". $this->queryString ."%'" );

        while( $row = $this->db->fetch( $query ) )
        {
            $this->results[] = $row['location_id'];
        }
        
        $this->model_location = new Classes_Location;
        
        if( count( $this->results ) )
            $this->layout->locations = $this->model_location->fetchById( $this->results );
        
        $this->layout->selected = $this->params['selected'];
        $this->layout->content = array( 'script' => 'search/locationsmini' );
    }
    
    private function searchEvents()
    {
        $query = $this->db->query( "SELECT event_id FROM fab_events WHERE event_title LIKE '%". $this->queryString ."%'" );

        while( $row = $this->db->fetch( $query ) )
        {
            $this->results[] = $row['event_id'];
        }
        
        $this->model_event = new Classes_Event;
        
        if( count( $this->results ) )
            $this->layout->events = $this->model_event->fetchById( $this->results );
        
        $this->layout->content = array( 'script' => 'search/events' );
    }    
}